home *** CD-ROM | disk | FTP | other *** search
- Path: news.microsoft.com!news
- From: warlok@siu.edu (Jon Fincher)
- Newsgroups: comp.lang.c
- Subject: Re: Problem with c code, please help!
- Date: 19 Jan 1996 22:57:30 GMT
- Organization: Microsoft Corp.
- Message-ID: <4dp7kq$ohm@news.microsoft.com>
- References: <surgsw-1901960148530001@128.206.206.86>
- NNTP-Posting-Host: 157.54.53.180
- Mime-Version: 1.0
- Content-Type: Text/Plain; charset=US-ASCII
- X-Newsreader: WinVN 0.99.7
-
- In article <surgsw-1901960148530001@128.206.206.86>,
- surgsw@mizzou1.missouri.edu says...
- >
- >I have been trying to get this very simple piece of code to work for
- >hours. What is the problem???????
- >
- >#include <stdio.h>
- >
- >main()
- >{
- > int i=0;
- > char word[100], c;
- > printf("Enter a word: ");
- > while( (c = getchar()) != '\n') {
- > *word = c;
- > word == word + 1;
- > }
- > printf("You entered: ");
- > *word = 0;
- > while( *word != '\n' )
- > {
- > putchar( *word );
- > word == word + 1;
- > }
- >}
-
- Well, the == is not assignment, it's comparitive equivalence. the = assigns
- one value to a variable, the == compares the two sides and returns TRUE or
- FALSE (NOT 0 or 0).
-
- Also, you have declared word to be a constant array, i.e. you dimensioned it
- as a declaration. You'll never be able to do pointer arithmetic on it. To do
- what you want, try this....
-
- #include <stdio.h>
-
- main()
- {
- int i=0;
- char word[100], c;
- char *w; //NEW LINE!!!!
- w = word; //NEW LINE!!!!
- printf("Enter a word: ");
- while( (c = getchar()) != '\n') {
- *w = c; //CHANGED
- w++; //CHANGED
- }
- printf("You entered: ");
- *w = 0; //CHANGED
- while( *w != '\n' ) //CHANGED
- {
- putchar( *w ); //CHANGED
- w++; //CHANGED
- }
- }
-
- Note in this example I created a char * to walk through the array. You could
- have easily done this using an integer array index and incrementing it rather
- than a walking pointer.
-
- Jon
-
-